'use strict';

const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
const awaitWriteStream = require('await-stream-ready').write;
const sendToWormhole = require('stream-wormhole');
class UserController extends Controller {
  /**
   * 用户信息设置相关
   */
  async getUserInfo() {
    const ctx = this.ctx;
    const userId = ctx.params.id;
    const { user } = await ctx.service.user.findUserById(userId);
    return ctx.helper.success({ ctx, data: user });
  }

  async updateUserInfo() {
    const { ctx } = this;
    const userInfoRule = {
      username: { type: 'string', trim: true },
      phone: { type: 'string', trim: true },
      position: { type: 'string', trim: true },
      introduction: { type: 'string', trim: true },
      avatar_url: { type: 'string', trim: true },
    };
    const data = ctx.request.body;
    ctx.validate(userInfoRule, data);
    const id = ctx.state.user.id;
    const { updateSuccess } = await ctx.service.user.updateUserInfo(id, data);
    return ctx.helper.success({
      ctx,
      msg: updateSuccess ? '修改成功' : '修改失败',
    });
  }

  async updateUserSetting() {
    const { ctx } = this;
    const userSettingRule = {
      theme_color: { type: 'string', trim: true },
      dark_mode: { type: 'number', trim: true },
      font_size: { type: 'number', trim: true },
    };
    const data = ctx.request.body;
    ctx.validate(userSettingRule, data);
    const id = ctx.state.user.id;
    const { updateSuccess } = await ctx.service.user.updateUserSetting(
      id,
      data
    );
    return ctx.helper.success({
      ctx,
      msg: updateSuccess ? '修改成功' : '修改失败',
    });
  }

  async updateUserPassword() {
    const { ctx } = this;
    const userPasswordRule = {
      password: 'string',
    };
    const data = ctx.request.body;
    ctx.validate(userPasswordRule, data);
    const id = ctx.state.user.id;
    const { updateSuccess } = await ctx.service.user.updateUserPassword(
      id,
      data
    );
    return ctx.helper.success({
      ctx,
      msg: updateSuccess ? '修改成功' : '修改失败',
    });
  }

  async getSign() {
    const { ctx } = this;
    const userId = ctx.state.user.id;
    const { today } = await ctx.service.user.getSign(userId);
    return ctx.helper.success({
      ctx,
      data: { today },
    });
  }

  async sign() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { signSuccess } = await ctx.service.user.sign(id);
    return ctx.helper.success({
      ctx,
      msg: signSuccess ? '签到成功' : '您已签到或签到失败',
    });
  }

  async uploadPic() {
    const { ctx } = this;
    const stream = await ctx.getFileStream();
    const filename = stream.filename;
    const target = path.join('app/public/image', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      return ctx.helper.paramErr(err);
    }
    return ctx.helper.success({
      ctx,
      data: { path: `/public/image/${filename}` },
    });
  }

  /**
   * 用户文章相关
   */

  async searchUserEssayList() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const { list } = await ctx.service.user.searchUserEssayList(userId);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async essayCommentLike() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.essayCommentLike(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async essayLike() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.essayLike(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async essayCollect() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.essayCollect(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  /**
   * 用户力扣区相关
   */
  async lcCollect() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.lcCollect(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async lcLike() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.lcLike(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  /**
   * 用户话题相关
   */

  async getUserTopicList() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.getUserTopicList(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  /**
   * 用户关注相关
   */

  async getFollowList() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.getFollowList(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getFollowedList() {
    const { ctx } = this;
    const id = ctx.state.user.id;
    const { list } = await ctx.service.user.getFollowedList(id);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async searchFollowList() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const { list } = await ctx.service.user.getFollowList(userId);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async searchFollowedList() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const { list } = await ctx.service.user.getFollowedList(userId);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async followUser() {
    const { ctx } = this;
    const followerId = ctx.params.id;
    const userId = ctx.state.user.id;
    if (followerId && userId) {
      const { insertSuccess } = await ctx.service.user.followUser(
        Number(followerId),
        Number(userId)
      );
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '关注成功' : '关注失败',
      });
    }
    return ctx.helper.paramErr({ ctx });
  }

  async cancelFollowTopic() {
    const { ctx } = this;
    const followerId = ctx.params.id;
    const userId = ctx.state.user.id;
    if (followerId && userId) {
      const { deleteSuccess } = await ctx.service.user.cancelFollowTopic(
        Number(followerId),
        Number(userId)
      );
      return ctx.helper.success({
        ctx,
        msg: deleteSuccess ? '取消关注成功' : '取消关注失败',
      });
    }
  }
}

module.exports = UserController;
